//
//  CommentViewController.m
//  第六组电影项目
//
//  Created by mini on 14-7-3.
//  Copyright (c) 2014年 www.skedu.com.cn北京尚德智远科技有限公司. All rights reserved.
//

#import "CommentViewController.h"
#import "CommentCell.h"
#import "CommentInfoModel.h"
#import "CommentHeadModel.h"
#import "WXNetworkService.h"
#import "MainViewController.h"
#import <MediaPlayer/MediaPlayer.h>

@interface CommentViewController ()
-(void)initHeaderView;
-(void)requestHeaderData;
-(void)refreshHeadData;
-(void)requestCommentData;
-(void)refreshCommentData;

@end

@implementation CommentViewController

-(void)loadView
{
    [super loadView];
    self.title = @"电影详情";
    
    commentTableView=[[UITableView alloc]initWithFrame:CGRectMake(0,0, kDeviceWidth, kDeviceHeight-20-44) style:UITableViewStylePlain];
    commentTableView.dataSource=self;
    commentTableView.delegate=self;
    commentTableView.hidden=YES;
    [self.view addSubview:commentTableView];
    [self initHeaderView];

}

-(void)initHeaderView
{
    //头部视图
    headerView=[[UIView alloc]initWithFrame:CGRectZero];
    
    //图像
    UIImageView *headImage=[[UIImageView alloc]initWithFrame:CGRectMake(10, 10, 110, 150)];
    headImage.tag=2001;
    [headerView addSubview:headImage];
    
    //标题
    UILabel *title=[[UILabel alloc]initWithFrame:CGRectMake(headImage.right+10,headImage.top,180,30)];
    title.font=[UIFont boldSystemFontOfSize:16];
    title.textColor=[UIColor whiteColor];
    title.tag=2002;
    [headerView addSubview:title];
    
    //导演
    UILabel *director=[[UILabel alloc]initWithFrame:CGRectMake(headImage.right+10,title.bottom,180,30)];
    director.font=[UIFont boldSystemFontOfSize:16];
    director.textColor=[UIColor whiteColor];
    director.tag=2003;
    [headerView addSubview:director];
    
    //演员
    UILabel *actor=[[UILabel alloc]initWithFrame:CGRectMake(headImage.right+10,director.bottom,180,30)];
    actor.font=[UIFont boldSystemFontOfSize:16];
    actor.textColor=[UIColor whiteColor];
    actor.tag=2004;
    [headerView addSubview:actor];
    
    //类型
    UILabel *type=[[UILabel alloc]initWithFrame:CGRectMake(headImage.right+10,actor.bottom,180,30)];
    type.font=[UIFont boldSystemFontOfSize:16];
    type.textColor=[UIColor whiteColor];
    type.tag=2005;
    [headerView addSubview:type];
    
    //上映时间
    UILabel *releaseDate=[[UILabel alloc]initWithFrame:CGRectMake(headImage.right+10,type.bottom,180,30)];
    releaseDate.font=[UIFont boldSystemFontOfSize:16];
    releaseDate.textColor=[UIColor whiteColor];
    releaseDate.tag=2006;
    [headerView addSubview:releaseDate];
    
    //想看按钮
    UIButton *interestButton=[UIButton buttonWithType:UIButtonTypeCustom];
    interestButton.frame=CGRectMake(20, headImage.bottom+10, 140, 40);
    [interestButton setBackgroundImage:[UIImage imageNamed:@"xk"] forState:UIControlStateNormal];
    [interestButton setBackgroundImage:[UIImage imageNamed:@"xk_on"] forState:UIControlStateHighlighted];
    [interestButton setTitle:@"想看" forState:UIControlStateNormal];
    [interestButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [interestButton addTarget:self action:@selector(action1) forControlEvents:UIControlEventTouchUpInside];
    [headerView addSubview:interestButton];
    
    //详细按钮
    UIButton *detailButton=[UIButton buttonWithType:UIButtonTypeCustom];
    detailButton.frame=CGRectMake(interestButton.right,headImage.bottom+10, 140, 40);
    [detailButton setBackgroundImage:[UIImage imageNamed:@"ypf"] forState:UIControlStateNormal];
    [detailButton setBackgroundImage:[UIImage imageNamed:@"ypf_on"] forState:UIControlStateHighlighted];
    [detailButton setTitle:@"详细" forState:UIControlStateNormal];
    [detailButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [detailButton addTarget:self action:@selector(action2) forControlEvents:UIControlEventTouchUpInside];
    [headerView addSubview:detailButton];
    
    //表视图
    UITableView *tableView=[[UITableView alloc]initWithFrame:CGRectMake(10, detailButton.bottom, 300, 270) style:UITableViewStyleGrouped];
    tableView.dataSource=self;
    tableView.delegate=self;
    tableView.tag=2007;
    tableView.scrollEnabled=NO;
    [headerView addSubview:tableView];
    headerView.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:@"bg_main"]];
    headerView.height=tableView.bottom+10;
    commentTableView.tableHeaderView=headerView;
    
}
-(void)action1
{
    NSDictionary *dic=[headModel.videos objectAtIndex:1];
    NSString *str=[dic objectForKey:@"url"];

    NSURL *url=[NSURL URLWithString:str];
    cinemaPlay=[[MPMoviePlayerViewController alloc]initWithContentURL:url];
    cinemaPlay.moviePlayer.controlStyle=MPMovieControlStyleFullscreen;
    [self presentMoviePlayerViewControllerAnimated:cinemaPlay];
    //[self presentViewController:cinemaPlay animated:YES completion:nil];
    
}
-(void)action2
{
    NSDictionary *dic=[headModel.videos objectAtIndex:0];
    NSString *str=[dic objectForKey:@"url"];
    NSURL *url=[NSURL URLWithString:str];
    cinemaPlay=[[MPMoviePlayerViewController alloc]initWithContentURL:url];
    cinemaPlay.moviePlayer.controlStyle=MPMovieControlStyleFullscreen;
    [self presentMoviePlayerViewControllerAnimated:cinemaPlay];
    
}

- (void)viewDidLoad
{
    [super viewDidLoad];
     [self requestCommentData];
    
    [self requestHeaderData];
   
	// Do any additional setup after loading the view.
}
-(void)requestHeaderData
{
    NSDictionary *dic=[WXNetworkService movieInfoData];
    headModel=[[CommentHeadModel alloc]initWithContent:dic];
    [self refreshHeadData];
}
-(void)refreshHeadData
{
    UIImageView *headImage=(UIImageView *)[headerView viewWithTag:2001];
    [headImage setImageWithURL:[NSURL URLWithString:headModel.image]];
    
    UILabel *title=(UILabel *)[headerView viewWithTag:2002];
    title.text=headModel.titleCn;
    
    UILabel *director=(UILabel *)[headerView viewWithTag:2003];
    NSString *directorLabel=[headModel.directors componentsJoinedByString:@","];
    director.text=directorLabel;
    
    UILabel *actor=(UILabel *)[headerView viewWithTag:2004];
    NSString *actorLabel=[headModel.actors componentsJoinedByString:@","];
    actor.text=actorLabel;
    
    UILabel *type=(UILabel *)[headerView viewWithTag:2005];
    NSString *typeLabel=[headModel.type componentsJoinedByString:@","];
    type.text=typeLabel;
    
    UILabel *releaseDate=(UILabel *)[headerView viewWithTag:2006];
    NSArray *releaseArray=[headModel.releaseDate allValues];
    NSString *releaseLabel=[releaseArray componentsJoinedByString:@","];
    releaseDate.text=releaseLabel;
    
    UITableView *tableView=(UITableView *)[headerView viewWithTag:2007];
    [tableView reloadData];
}
-(void)requestCommentData
{
    NSArray *data=[WXNetworkService movieCommentData];
    commentArray=[[NSMutableArray alloc]initWithCapacity:data.count];
    for (id dic in data)
    {
        CommentInfoModel *commentInfoModel=[[CommentInfoModel alloc]initWithContent:dic];
        [commentArray addObject:commentInfoModel];
    }
    [self refreshCommentData];
}
-(void)refreshCommentData
{
    commentTableView.hidden=NO;
    [commentTableView reloadData];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView==commentTableView)
    {
        return 10;
    }
    else
    {
        return 4;
    }
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationItem.hidesBackButton = YES;
    self.tabBarController.tabBar.hidden = YES;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView==commentTableView)
    {
        UITableViewCell *cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];
        
        return cell.frame.size.height;
    }
    
    else
    {
        if (indexPath.row==0 || indexPath.row==3)
        {
            return 70;
        }
        else
        {
            return 50;
        }
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView==commentTableView)
    {
        static NSString *identifier=@"cell";
        CommentCell *cell=[tableView dequeueReusableCellWithIdentifier:identifier];
        if (cell==nil)
        {
            cell=[[CommentCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        }
        cell.commentInfoModel=commentArray[indexPath.row];
        cell.content.text = [commentArray[indexPath.row] content];
        
        
                 /*单元格高度自适应*/
        //方法1
        CGSize size = [cell.content sizeThatFits:CGSizeMake(220, MAXFLOAT)];
        double height = size.height;
        
        //方法2
        /*
         NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:cell.content.font,NSFontAttributeName, nil];
         double height = [cell.content.text boundingRectWithSize:CGSizeMake(220, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:dict context:nil].size.height;
         */
        CGRect rect = cell.frame;
        rect.size.height = height + 50;
        cell.frame = rect;
        return cell;
        
    }
    else
    {
        UITableViewCell *cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
        if (indexPath.row==0)
        {
            int x=0;
            for (int i=0; i<headModel.images.count; i++)
            {
                UIImageView *image=[[UIImageView alloc]initWithFrame:CGRectMake(10+x, 5, 60, 60)];
                [image setImageWithURL:[NSURL URLWithString:headModel.images[i]]];
                [cell.contentView addSubview:image];
                x+=(image.width+5);
                
            }
        }
        else if (indexPath.row==1)
        {
            
            cell.textLabel.text=headModel.content;
        }
        else if (indexPath.row==2)
        {
            NSString *str=[headModel.actors componentsJoinedByString:@","];
            cell.textLabel.text=[NSString stringWithFormat:@"职员表：%@",str];
        }
        else
        {
            int x=0;
            for (int i=0; i<headModel.videos.count; i++)
            {
                UIImageView *image=[[UIImageView alloc]initWithFrame:CGRectMake(15+x, 5, 80, 60)];
                NSDictionary *dic=[headModel.videos objectAtIndex:i];
                NSString *str=[dic objectForKey:@"image"];
                [image setImageWithURL:[NSURL URLWithString:str]];
                [cell.contentView addSubview:image];
                x+=(image.width+5);
            }
        }
        return cell;
        
    }

}



@end
